// 分类数据相关业务代码
import { getTopCategory } from '@/api/category'
import { ref, onMounted } from "vue"
import { onBeforeRouteUpdate, useRoute } from 'vue-router';

export function useCategory() {
    // 获取二级分类的数据
    const route = useRoute()
    const categoryData = ref({})
    const getTopCategoryData = (id) => {
        getTopCategory(id).then(res => {
            categoryData.value = res.result
        })
    }
    onMounted(() => getTopCategoryData(route.params.id))
    // 路由切换（参数变化）重新获取新的数据
    onBeforeRouteUpdate((to) => {
        getTopCategoryData(to.params.id)
    })
    return {
        categoryData
    }
}